#!/bin/bash

#定义Runtime和unitypackage的版本号。为pcsdk打tag并提交。
echo "
使用方法：
1、将脚本放到PicoSDK_PC_Runtime和PicoSDK_Unity同级目录
2、PicoSDK_PC_Runtime编译完成后，将中文版的PicoVR放到PicoSDK_PC_Runtime/Chinese下
3、执行脚本，按照提示输入PicoSDK_PC_Runtime和PicoSDK_Unity的版本号"

echo "
请输入PicoSDK_PC_Runtime的版本号，例：v0.4.0： "
read pcsdk_v

#判断PicoSDK_PC_Runtime中是否存在同名的tag
#cd PicoSDK_PC_Runtime
#for Oldpcsdk_v in `git tag` ; do
#	if [[ ${pcsdk_v} != ${Oldpcsdk_v}  ]]; then
#		echo "检查中Runtime中的是否存在同名tag..." >/dev/null
#	else
#		echo "failed:Runtime中存在同名tag，请重新输入"
#		cd ..
#		./pcsdk_to_unity.sh
#		exit 2
#	fi
#done
#tag判断正确后，为PicoSDK_PC_Runtime打tag
#git tag -a "${pcsdk_v}" -m "for unitypackage_${unity_v}"
#git push --tags >/dev/null 2>&1

#判断PicoSDK_Unity中是否存在同名的tag
#cd ../PicoSDK_Unity
#for Oldunity_v in `git tag | awk -F "_" '{print $1}'` ; do
#	if [[ ${unity_v} != ${Oldunity_v}  ]];then
#		echo "检查中PicoSDK_Unity中的是否存在同名tag..." >/dev/null
#	else
#		echo "failed:PicoSDK_Unity中存在同名tag，请重新输入"
#		cd ..
#		./pcsdk_to_unity.sh
#		exit 3
#	fi
#done


#拷贝Runtime的PicoPlugin.dll到PicoSDK_Unity的MainUnity52分支之前,更新Unity的代码，
cd D:/scmbuild/PicoSDK_Unity/
git checkout . && git clean -xdf && git reset --hard HEAD && git checkout MainUnity52 && git pull
#判断是否更新成功，成功则打印最新一条提交的commit时间、哈希和提交信息;失败则退出脚本并返回4
if [[ $? -eq 0 ]] ; then
	echo "
已更新PicoSDK_Unity的MainUnity52分支，最近的5条提交是:
`git log --pretty=format:"[%h] %cd | %s [%an]"  --date=iso -5`"
 else
 	echo "
failed:git pull MainUnity52分支失败！"
        exit 2
fi
#开始拷贝
cp -f D:/scmbuild/PicoSDK_PC_Runtime/English/PicoVR/Bin/Win32/PicoPlugin.dll Assets\Plugins\x86\PicoPlugin.dll
cp -f D:/scmbuild/PicoSDK_PC_Runtime/English/PicoVR/Bin/Win64/PicoPlugin.dll Assets\Plugins\x86_64\PicoPlugin.dll
git add .
git commit -m "update pcsdk to ${pcsdk_v} on MainUnity52"
git pull
if [[ $? -eq 0 ]] ; then
	git push origin MainUnity52
#	git tag -a "${unity_v}_MainUnity52" -m "update pcsdk to ${pcsdk_v}"
else
	echo "failed:在MainUnity52分支commit后git pull失败，请确认检查"
	exit 5
fi
#拷贝Runtime的PicoPlugin.dll到PicoSDK_Unity的LegacyUnity分支
git checkout . && git clean -xdf && git checkout LegacyUnity && git pull
if [[ $? -eq 0 ]] ; then
        echo "
已更新PicoSDK_Unity的LegacyUnity分支，当前所在提交:
`git log --pretty=format:"[%h] %cd | %s [%an]"  --date=iso -5`"
 else
         echo "
failed:git pull LegacyUnity分支失败！"
	 exit 6
fi
cp -f ../PicoSDK_PC_Runtime/Chinese/PicoVR/Bin/Win32/PicoPlugin.dll Assets\Plugins\x86\PicoPlugin.dll
cp -f ../PicoSDK_PC_Runtime/Chinese/PicoVR/Bin/Win64/PicoPlugin.dll Assets\Plugins\x86_64\PicoPlugin.dll
git add .
git commit -m "update pcsdk to ${pcsdk_v} on LegacyUnity" 
git pull
if [[ $? -eq 0 ]] ; then
	git push origin LegacyUnity
#	git tag -a "${unity_v}_LegacyUnity" -m "update pcsdk to ${pcsdk_v}"
else
	echo "failed:在LegacyUnity分支commit后git pull失败，请确认检查"
	exit 7
fi

#git push --tags >/dev/null 2>&1
if [[ $? -eq 0 ]] ; then
	echo "
#更新PicoSDK_PC_Runtime的tag到${pcsdk_v}
#更新PicoSDK_Unity的tag到${unity_v}_MainUnity52和${unity_v}_LegacyUnity

已成功将PC_SDK更新到PicoSDK_Unity的两个分支中，
#并对两个库打了tag标记。

请继续其他更新SDK的操作，或者开始打包unitypackage..."

else
	echo "git push到LegacyUnity失败"
fi
